﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Tools {
    /// <summary>
    /// 时间帮助类
    /// </summary>
    public static class DateTimeHelper {

        /// <summary>
        /// 时间戳转为C#格式时间
        /// </summary>
        /// <param name="unixTimeStamp">Unix时间戳格式</param>
        /// <returns>C#格式时间</returns>
        public static DateTime GetDateTime(double unixTimeStamp) {
            var dtDateTime = new DateTime(1970, 1, 1, 0, 0, 0, 0);
            dtDateTime = dtDateTime.AddSeconds(unixTimeStamp).ToLocalTime();
            return dtDateTime;
        }

        public static DateTime GetDateTimeForMilliseconds(double unixTimeStamp) {
            var dtDateTime = new DateTime(1970, 1, 1, 0, 0, 0, 0);
            dtDateTime = dtDateTime.AddMilliseconds(unixTimeStamp).ToLocalTime();
            return dtDateTime;
        }


        /// <summary>
        ///     获得当前时间的时间戳（秒）
        /// </summary>
        /// <returns></returns>
        public static long GetTimeStamp() {
            TimeSpan ts = DateTime.UtcNow - new DateTime(1970, 1, 1, 0, 0, 0, 0);
            return Convert.ToInt64(ts.TotalSeconds);
        }

        /// <summary>
        ///     获得当前时间的时间戳(毫秒)
        /// </summary>
        /// <returns></returns>
        public static long GetTimeStampForMilliseconds() {
            TimeSpan ts = DateTime.UtcNow - new DateTime(1970, 1, 1, 0, 0, 0, 0);
            return Convert.ToInt64(ts.TotalMilliseconds);
        }

        public static long GetTimeStampForMilliseconds(long time) {
            TimeSpan ts = DateTime.UtcNow.AddMilliseconds(time) - new DateTime(1970, 1, 1, 0, 0, 0, 0);
            return Convert.ToInt64(ts.TotalMilliseconds);
        }

        /// <summary>
        ///     获得当前时间 + time的时间戳
        /// </summary>
        /// <param name="time">毫秒</param>
        /// <returns>时间戳</returns>
        public static long GetTimeStamp(long time) {
            TimeSpan ts = DateTime.UtcNow.AddMilliseconds(time) - new DateTime(1970, 1, 1, 0, 0, 0, 0);
            return Convert.ToInt64(ts.TotalSeconds);
        }

        /// <summary>
        /// DateTime时间格式转换为Unix时间戳格式
        /// 单位：秒
        /// </summary>
        /// <param name="time"> DateTime时间格式</param>
        /// <returns>Unix时间戳格式</returns>
        public static int ConvertDateTimeInt(DateTime time) {
            DateTime startTime = TimeZone.CurrentTimeZone.ToLocalTime(new DateTime(1970, 1, 1, 0, 0, 0, 0));
            return (int)(time - startTime).TotalSeconds;
        }

        /// <summary>
        /// DateTime时间格式转换为Unix时间戳格式
        /// 单位：毫秒
        /// </summary>
        /// <param name="time"></param>
        /// <returns></returns>
        public static long ConvertDateTimeLong(DateTime time) {
            DateTime startTime = TimeZone.CurrentTimeZone.ToLocalTime(new DateTime(1970, 1, 1, 0, 0, 0, 0));
            return (long)(time - startTime).TotalMilliseconds;
        }

        /// <summary>
        /// 时间段比较（无交集），开区间
        /// </summary>
        /// <param name="srcBeginTime"></param>
        /// <param name="srcEndTime"></param>
        /// <param name="targBeginTime"></param>
        /// <param name="targEndTime"></param>
        /// <returns>T:不再范围内 F：在范围内</returns>
        public static bool Compare(DateTime? srcBeginTime, DateTime? srcEndTime, DateTime targBeginTime, DateTime targEndTime) {
            if (srcBeginTime > targEndTime)//（源）开始时间大于结束时间
                return true;

            if (srcEndTime < targBeginTime)//（源）结束时间小于开始时间
                return true; ;

            return false;
        }
    }
}
